package com.tmeone.erpone.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmeone.erpone.entity.JsonResult;
import com.tmeone.erpone.entity.TPrinter;
import com.tmeone.erpone.service.ITPrinterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
public class TPrinterController {
    @Autowired
    private ITPrinterService printerService;

    //查询所有打印机
    @GetMapping("/tolaserprinters")
    public JsonResult getPrinters(){
        return new JsonResult(1,printerService.list());
    }

    //打印机所有的数据查询并分页
    @GetMapping("/printers")
    public JsonResult getPrinterAll(@RequestParam(value = "current",required = false,defaultValue = "1")
                                                Integer current){
        IPage<TPrinter> arg = new Page<>(current,5);
        IPage<TPrinter> page = printerService.pageTPrinters(arg);
        return new JsonResult(1,page);
    }

    //添加打印机
    @PostMapping("/printer")
    public JsonResult savePrinter(@RequestBody TPrinter printer){
        printer.setId(UUID.randomUUID().toString().substring(0,10));
        printerService.save(printer);
        return new JsonResult(1,printer);
    }

    //批量删除
    @PostMapping("/printer/deletes")
    public JsonResult deletePrinters(@RequestBody List<String> ids){
        QueryWrapper<TPrinter> queryWrapper = new QueryWrapper<>();
        for (String id :ids) {
            queryWrapper.eq("id",id);
            queryWrapper.or();
        }
        printerService.remove(queryWrapper);
        return new JsonResult(1,"批量删除成功");
    }

    //单点删除
    @DeleteMapping("/printer/{id}")
    public JsonResult deletePrinter(@PathVariable("id") String id){
        printerService.removeById(id);
        return new  JsonResult(1,"删除成功");
    }

    //修改
    @PatchMapping("/printer")
    public JsonResult updatePrinter(@RequestBody TPrinter printer){
        printerService.updateById(printer);
        return new JsonResult(1,"修改成功");
    }

    //分页模糊查询
    @GetMapping("/printers/search")
    public JsonResult getPrinterBypNum(@RequestParam("search") String search,@RequestParam("current") int current){
        Page page = new Page(current,5);
        return new JsonResult(1,printerService.findPrinterBypNum(page,search));
    }
}
